package com.fingard.dsp.bank.directbank.zjrb01;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/4/26.
 */
public class ZJRB01Base extends DirectBase implements ITcpClient{


    protected String getCharset(){ return getBankConfig().getCharset("GB2312");}

    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {

    }

    protected void writeCommenHeader(XmlTextWriter xmlTextWriter,String opName){
        xmlTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding = \"GB2312\"?>");
        xmlTextWriter.writeStartElement("ROOT");
        xmlTextWriter.writeStartElement("opReq");
        xmlTextWriter.writeElementString("opName",opName);
    }

    protected void wirteReqBody(XmlTextWriter xmlTextWriter, XElement ReqParam, XElement ReqParamSet){
        //单笔操作的报文格式，如果是批量，这个标签的内容是一些访问的用户信息
        xmlTextWriter.writeXElement(ReqParam);
        //批量操作的报文格式
        if(ReqParamSet!=null){
            xmlTextWriter.writeXElement(ReqParamSet);
        }

    }

    protected String[] sendToBank(String p_reqStr)throws Exception{
        String[] result = {"",""};
        WriteBankLogStep2(p_reqStr);
//        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
//        TcpReceiver tmpReceiver = tmpClient.sendMessage(p_reqStr.getBytes("GB2312"));
//        result[0] = tmpReceiver.connState;
//        if (result[0].length() > 0) {
//            result[1] = tmpReceiver.getConnMsg();
//            return result;
//        }
//        if (tmpReceiver.recBytes != null) {
//            //result[1] = new String(tmpReceiver.recBytes, 0, tmpReceiver.recBytes.length, "utf-8");
//            result[1] = new String(tmpReceiver.recBytes, 0, tmpReceiver.recBytes.length, "GB2312");
//        }
       // p_reqStr = String.format("<textarea name=\"requestXML\" rows=\"10\" cols=\"80\">%s</textarea>",p_reqStr);
        p_reqStr = "requestXML="+p_reqStr;
        WebRequest webRequest = new WebRequest(bankFront.serverURL);
        result = webRequest.upload(p_reqStr.getBytes(getCharset()), getCharset());
        WriteBankLogStep3(result[1]);
        return result;
    }
    private static TransIDHelper idHelper = new TransIDHelper(1, 999);

    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.DateTime12Year2Format.format(tmpDateNow) + idHelper.getNextIDStr();
    }

}
